<template>
  <div class="app-container">
    <!-- 搜索栏 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="账本名字" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入账本名字" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="菜单状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 按钮栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:menu:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="" v-hasPermi="['system:menu:add']">删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="accountBookList" row-key="id">
      <el-table-column prop="id" label="ID" :show-overflow-tooltip="true" width="160"></el-table-column>
      <el-table-column prop="name" label="账本名字" :show-overflow-tooltip="true" width="160"></el-table-column>
      <el-table-column prop="status" label="账本状态" width="160">
        <template slot-scope="scope">
          <!-- 1是启用，2是停用 -->
          <span v-if="scope.row.status == 1">正常</span>
          <span v-if="scope.row.status == 2">删除</span>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="createUserAccount" label="创建人账户" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="remark" label="备注" align="center" width="200"></el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:menu:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:menu:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改菜单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="680px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="18" offset="0">
            <el-form-item label="账本名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入账本名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="18" offset="0">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" maxlength="100" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>

</template>


<script>
  import { listAccountBook, addAccountBook, delAccountBook } from "@/api/bill";

  export default {
    name: "accountBook",

    data() {
      return {
        // 遮罩层
        loading: true,
        // 是否显示弹出层
        open: false,
        // 表单参数
        form: {},
        // 弹出层标题
        title: "",
        // 显示搜索条件
        showSearch: true,
        accountBookList: [],
        queryParams: {
          name: undefined,
          status: undefined
        },
        // 账本状态数据字典
        statusOptions: [
          {
            "remark": "正常状态",
            "dictLabel": "正常",
            "dictValue": 1,
          },
          {
            "remark": "删除状态",
            "dictLabel": "删除",
            "dictValue": 2,
          }
        ],
        // 表单校验
        rules: {
          name: [
            { required: true, message: "账本名字不能为空", trigger: "blur" }
          ],

        }
      }
    },
    created() {
      this.getList();
    },
    methods: {
      // 重置form表单字段
      reset() {
        this.form = {
          id: undefined,
          name: '',
          remark: ''
        }
        // 对refname=form的整个表单进行重置，将所有字段值重置为初始值并移除校验结果
        this.resetForm("form");
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              updateAccountBook(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addAccountBook(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd(row) {
        // 重置表单
        this.reset();
        this.open = true;
        this.title = "添加账本";
      },
      /** 提交按钮 */
      submitForm: function () {
        console.log(this.form);
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              // 修改账簿
              updateUser(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              // 新增账簿
              addAccountBook(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      // 修改账簿
      handleUpdate() {
        // 重置表单
        this.reset();
        this.open = true;
        this.title = "修改账本";
      },
      // 获取账簿列表
      getList() {
        listAccountBook(this.queryParams).then(response => {
          this.accountBookList = response.rows
          this.loading = false;
        });
      }
    }
  }
</script>

<style scoped>
  .accountBooks {
    position: relative;
    float: left;
    margin: 20px;
    width: 300px;
    height: 300px;
    border: 3px solid #ccc;
    border-radius: 15px;
  }

  .addAccountBook {
    width: 294px;
    height: 294px;
  }

  .accountBooks span {
    position: absolute;
    top: auto;
    left: auto;
  }
</style>